this.name = "Oolite Shipyard Extension - Player Stations";
this.author = "Paul Wilkins";
this.copyright = "(C) 2009 Paul Wilkins";
this.description = "Converted from the 0.69 version of OSE-XML.plist";
this.version = "0.70";

this.startUp = function () {
    if (oolite.compareVersion("1.73.4") > 0)
    {
        // Oolite version is older than 1.74.
        log("script." + this.name, "This Player Stations script does not run under Oolite version " + oolite.versionString + ". Player Stations is disabled.");
        delete this.shipExitedWitchspace;
    }
};
this.playerStationIncome = function (type, galaxy) {
	var stationIncome = {
		'A': 4000, 'B': 8000, 'C': 12000, 'D': 21000, 'E': 20000, 'F': 36000,
		'G': 40000, 'H': 75000, 'I': 100000, 'J': 130000, 'K': 250000, 'L': 500000
		},
		projectedIncome = stationIncome[type],
		baseIncome = projectedIncome * 0.80,
		variableIncome = projectedIncome * 0.40 * Math.random(),
		stationIncome = baseIncome + variableIncome;
	
	// adjust for courier travel from station galaxy to your galaxy
	if (galaxyNumber !== galaxy) {
		stationIncome *= 0.95;
	}
	while (galaxyNumber !== galaxy) {
		stationIncome *= 0.97;
		galaxy = (galaxy + 1) % 8;
	}
	return stationIncome;
};
this.playerStationIncomeAndBounty = function () {
	var stationTypes = 'ABCDEFGHIJKL',
		stationsPerGalaxy = 'AB',
		galaxies = 'ABCDEFGH',
		stationBounty = {
			'A': 0, 'B': 0, 'C': 5, 'D': 20, 'E': -3, 'F': 40,
			'G': -6, 'H': 70, 'I': 0, 'J': -12, 'K': -6, 'L': -6
		},
		i, j, k,
		stationType, galaxyStation, galaxy,
		playerStation,
		bounty,
		totalBounty = 0;
	for (i = 0; i < stationTypes.length; i += 1) {
		for (j = 0; j < stationsPerGalaxy.length; j += 1) {
			for (k = 0; k < galaxies.length; k += 1) {
				stationType = stationTypes.charAt(i);
				galaxyStation = stationsPerGalaxy.charAt(j);
				galaxy = galaxies.charAt(k);
				playerStation = stationType + galaxyStation + galaxy;
				if (missionVariables['playerstation_' + playerStation] === "1") {
					player.credits += this.playerStationIncome(stationType, galaxy);
					totalBounty += stationBounty[stationType];
				}
			}
		}
	}
	if (player.bounty + totalBounty < 0) {
		player.bounty = 0;
	} else {
		player.bounty += totalBounty;
	}
};
this.shipExitedWitchspace = function () {
	var totalWeeks = Math.floor(clock.days / 7),
		dayPaid = missionVariables.playerStationDayPaid || 0,
		weeksPaid = Math.floor(dayPaid / 7),
		weeksToPay = totalWeeks - weeksPaid;
	if (dayPaid === 0) {
		weeksToPay = 1;
	};
	while (weeksToPay > 0 && weeksToPay--) {
		this.playerStationIncomeAndBounty();
	}
	missionVariables.playerStationDayPaid = clock.days;
};